// CppUnit-Tutorial
// Datei: btest.cc


#include <cppunit/CompilerOutputter.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/TestResult.h>
#include <cppunit/TestResultCollector.h>
#include <cppunit/TestRunner.h>
#include <cppunit/BriefTestProgressListener.h>
#include "include/debug_new.h"
#include <iostream>
#include "include/intToStr.h"

using namespace std;

class MemoryListener : public CPPUNIT_NS::TestListener
{
	int all;
public:
	MemoryListener() {
		this->all = 0;
	}
	void
	endTestRun( CPPUNIT_NS::Test *test,
	                            CPPUNIT_NS::TestResult *eventManager )
	{
		cout << (string)"Found (" + intToStr(all) +") Memory Leaks" << endl;
	}
	void startTest( CPPUNIT_NS::Test *test ) {
	  clearCounter();
	}
	void endTest( CPPUNIT_NS::Test *test ) {
		int leaks = getCounter();
		all = all + leaks;
		cout << " : " << leaks;
	}

};

int mainRun(int argc, char* argv[]) {
	// Informiert Test-Listener ueber Testresultate
	CPPUNIT_NS :: TestResult testresult;

	// Listener zum Sammeln der Testergebnisse registrieren
	CPPUNIT_NS :: TestResultCollector collectedresults;
	testresult.addListener (&collectedresults);

	MemoryListener memoryListener;
	testresult.addListener (&memoryListener);

	// Listener zur Ausgabe der Ergebnisse einzelner Tests
	CPPUNIT_NS :: BriefTestProgressListener progress;
	testresult.addListener (&progress);

	// Test-Suite ueber die Registry im Test-Runner einfuegen
	CPPUNIT_NS :: TestRunner testrunner;
	testrunner.addTest (CPPUNIT_NS :: TestFactoryRegistry :: getRegistry ().makeTest ());
	testrunner.run (testresult);

	// Resultate im Compiler-Format ausgeben
	CPPUNIT_NS :: CompilerOutputter compileroutputter (&collectedresults, std::cerr);
	compileroutputter.write ();

	// Rueckmeldung, ob Tests erfolgreich waren
	int res = collectedresults.wasSuccessful () ? 0 : 1;
	return res;
}

int main (int argc, char* argv[]) {
	int res = mainRun(argc,argv);

	/* Clear counter */
	eraseCounter();
	return res;
}
